package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private RoomAttrValueService roomAttrValueService;

    @Autowired
    private RoomFacilityService roomFacilityService;

    @Autowired
    private RoomLabelService roomLabelService;

    @Autowired
    private RoomLeaseTermService roomLeaseTermService;

    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;

    //保存或更新房间信息
    @Override
    public void saveOrUpdateRoomInfo(RoomSubmitVo roomSubmitVo) {
        //判断当前功能位添加或者修改
        boolean isUpdate = roomSubmitVo.getId() != null;
        //添加或修改房间信息,若为添加功能,MyBatisPulus会自动获取自增的主键,赋值给roomSubmitVo里面的id
        saveOrUpdate(roomSubmitVo);
        //获取房间id
        Long roomId = roomSubmitVo.getId();
        //判断是否为修改若为修改,将房间对应的图片,配套,标签,属性,支付方式,租期信息删除
        if(isUpdate){
            //删除房间对应的图片信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId,roomId);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

            //删除房间的标签信息
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomId);
            roomLabelService.remove(roomLabelLambdaQueryWrapper);

            //删除房间的配套信息
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getFacilityId,roomId);
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

            //删除房间的属性值信息
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

            //删除房间的支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomId);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

            //删除房间的租期信息
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);

        }

        //添加房间的对应的图片,配套,标签,属性,支付方式,租期信息

        //添加房间对应的图片信息
        List<GraphVo> graphVoList= roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList=new ArrayList<>();
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemId(roomId);
                graphInfo.setItemType(ItemType.ROOM);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //添加房间对应的标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelInfoIds)){
            List<RoomLabel> roomLabelList=new ArrayList<>();
            for (Long labelInfoId : labelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder()
                        .roomId(roomId).labelId(labelInfoId).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }

        //添加房间对应的属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> roomAttrValueList=new ArrayList<>();
            for (Long attrValueId : attrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                        .roomId(roomId).attrValueId(attrValueId).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }

        //添加房间对应的配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilityList=new ArrayList<>();
            for (Long facilityInfoId : facilityInfoIds) {
                RoomFacility roomFacility= RoomFacility.builder()
                        .roomId(roomId).facilityId(facilityInfoId).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }

        //添加房间对应的支付信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypeList=new ArrayList<>();
            for (Long paymentTypeId : paymentTypeIds) {
                RoomPaymentType roomPaymentType =RoomPaymentType.builder()
                        .roomId(roomId).paymentTypeId(paymentTypeId).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }

        //添加房间对应的租期信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        if (!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTermList=new ArrayList<>();
            for (Long leaseTermId : leaseTermIds) {
                RoomLeaseTerm roomLeaseTerm=RoomLeaseTerm.builder()
                        .roomId(roomId).leaseTermId(leaseTermId).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }




    }

    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    //根据条件分页查询房间列表
    @Override
    public IPage<RoomItemVo> findRoomItemVopage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.findRoomItemVopage(page,queryVo);
    }

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;

    @Autowired
    private RoomFacilityMapper roomFacilityMapper;

    @Autowired
    private RoomLabelMapper roomLabelMapper;

    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;

    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;

    // 根据ID获取房间详细信息
    @Override
    public RoomDetailVo findRoomDetailVoById(Long roomId) {


        //根据房间id查询房间信息
        RoomInfo roomInfo = getById(roomId);
        //根据房间id查询对应的公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());

        //根据ItemType和ItemId查询公寓所对应的图片信息
        List<GraphVo> graphVoList = graphInfoMapper.findGraphVoListByItemTypeAndItemId(ItemType.ROOM, roomId);

        //查询房间所对应的属性信息
        List<AttrValueVo> attrValueVoList=roomAttrValueMapper.findAttrValueVoListByRoomId(roomId);
        //查询房间所对应的配套信息
        List<FacilityInfo> facilityInfoList = roomFacilityMapper.findFacilityInfoListByRoomId(roomId);
        //查询房间所对应的标签信息
        List<LabelInfo> labelInfoList = roomLabelMapper.findLabelInfoListByRoomId(roomId);
        //查询房间所对应的支付方式信息
        List<PaymentType> paymentTypeList = roomPaymentTypeMapper.findPaymentTypeListByRoomId(roomId);
        //查询房间所对应的租期信息
        List<LeaseTerm> leaseTermList = roomLeaseTermMapper.findLeaseTermListByRoomId(roomId);

        //创建RoomDetailVo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);




        return roomDetailVo;
    }

    //删除
    @Override
    public void removeRoomInfoById(Long roomId) {
        //删除房间对应的图片信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM).eq(GraphInfo::getItemId,roomId);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

        //删除房间的标签信息
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomId);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);

        //删除房间的配套信息
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getFacilityId,roomId);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);

        //删除房间的属性值信息
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomId);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);

        //删除房间的支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomId);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);

        //删除房间的租期信息
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }


}




